package cn.yqn.system.controller;

import cn.yqn.basic.utils.JsonResult;
import cn.yqn.basic.utils.PageList;
import cn.yqn.system.domain.DictionaryDetail;
import cn.yqn.system.query.DictionaryDetailQuery;
import cn.yqn.system.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping
    public List<DictionaryDetail> loadAll() {
        return dictionaryDetailService.loadAll();
    }

    @GetMapping("/{id}")
    public DictionaryDetail loadById(@PathVariable("id") Long id) {
        return dictionaryDetailService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            dictionaryDetailService.remove(id);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            System.out.println(ids);
            dictionaryDetailService.patchDel(ids);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PutMapping
    public JsonResult saveOrUpdate(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.save(dictionaryDetail);
            } else{
                dictionaryDetailService.update(dictionaryDetail);
            }
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery) {
        try {
            return dictionaryDetailService.pageQuery(dictionaryDetailQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<DictionaryDetail>();
        }
    }

    @GetMapping("/findType/{id}")
    public List<DictionaryDetail> findType(@PathVariable("id") Long id) {
        return dictionaryDetailService.findType(id);
    }
}
